<!DOCTYPE html>
<html lang="zh-cn">
    <head>
        <meta charset="UTF-8"/>
        <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
        <title>绑定函数-配合setTimeout</title>
    </head>
    <body>
        <h1>绑定函数-配合setTimeout</h1>
        <p>来源:<a href="https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/bind">https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Function/bind</a></p>
        <script>
            var petalCount = 30;
            function LateBloomer() {
              this.petalCount = Math.ceil(Math.random() * 12) + 1;
            }

            // Declare bloom after a delay of 1 second
            LateBloomer.prototype.bloom = function() {
              window.setTimeout(this.declare.bind(this), 1000);
            };

            LateBloomer.prototype.declare = function() {
              console.log('I am a beautiful flower with ' +
                this.petalCount + ' petals!');
            };

            var flower = new LateBloomer();
            flower.bloom();  // 一秒钟后, 调用'declare'方法
        </script>
    </body>
</html>